package net.zxteam.solutions.java.rs_sync;


import java.io.StringReader;
import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="sync")
final class SyncInfo {

	static final JAXBContext jaxbCtx;

	static {

		JAXBContext c = null;
		try { c = JAXBContext.newInstance(SyncInfo.class); }
		catch(Exception ex) { c = null; }

		jaxbCtx = c;
	}
	
	public static SyncInfo deserialize(String xmlData) throws JAXBException {
		
		Unmarshaller unm = jaxbCtx.createUnmarshaller();
		StringReader sr = new StringReader(xmlData);
		SyncInfo ti = (SyncInfo)unm.unmarshal(sr);
		return ti;
	}
	
	public static String serialize(SyncInfo syncInfo) throws JAXBException {
		
		Marshaller m = jaxbCtx.createMarshaller();
		StringWriter sw = new StringWriter();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
		m.setProperty(Marshaller.JAXB_FRAGMENT, true);
		m.marshal(syncInfo,sw);
		return sw.toString();
	}

	@XmlAttribute(name="rev")
	public long revision;
}